package com.supor.aiot.mine.helper;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.supor.aiot.mine.bean.ImageItem;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FeedbackImageCache {
    private static final String TAG;
    private static FeedbackImageCache instance;
    private List<String> drr;
    private List<ImageItem> selectItem;
    // private List<Bitmap> bitmaps;
    private int max;

    static {
        TAG = FeedbackImageCache.class.getName();
        FeedbackImageCache.instance = new FeedbackImageCache();
    }

    private FeedbackImageCache() {
        this.max = 0;
        this.drr = new ArrayList<String>();
        this.selectItem = new ArrayList<>();
        // bitmaps = new ArrayList<>();
    }

    public static FeedbackImageCache getInstance() {
        return FeedbackImageCache.instance;
    }

    public static Bitmap revisionImageSize(final String path) {
        try {
            BufferedInputStream in = new BufferedInputStream(new FileInputStream(
                    new File(path)));
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(in, null, options);
            in.close();
            int i = 0;
            Bitmap bitmap = null;
            while (true) {
                if ((options.outWidth >> i <= 1000)
                        && (options.outHeight >> i <= 1000)) {
                    in = new BufferedInputStream(
                            new FileInputStream(new File(path)));
                    options.inSampleSize = (int) Math.pow(2.0D, i);
                    options.inJustDecodeBounds = false;
                    bitmap = BitmapFactory.decodeStream(in, null, options);
                    break;
                }
                i += 1;
            }
            return bitmap;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean fileOverSize(List<ImageItem> itemList) {
        for (ImageItem imageItem : itemList) {
            if (imageItem.size > 50 * 1024 * 1024) {
                return true;
            }
        }
        return false;
    }

    public void cleanBimp() {
        this.setMax(0);
        this.getDrr().clear();
        this.getSelectItem().clear();
        // for (Bitmap bitmap : bitmaps) {
        //     bitmap.recycle();
        // }
        // getBitmaps().clear();
    }

    public List<ImageItem> getSelectItem() {
        return selectItem;
    }

    public List<String> getDrr() {
        return this.drr;
    }

    public int getMax() {
        return this.max;
    }

    public void setDrr(final List<String> drr) {
        this.drr = drr;
    }

    public void setSelectItem(List<ImageItem> selectItem) {
        this.selectItem = selectItem;
    }

    public void setMax(final int max) {
        this.max = max;
    }

    // public List<Bitmap> getBitmaps() {
    //     return bitmaps;
    // }

    // public void setBitmaps(List<Bitmap> bitmaps) {
    //     this.bitmaps = bitmaps;
    // }
}
